package com.rushb.rushbproduct.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rushb.rushbproduct.constont.ErrorCode;
import com.rushb.rushbproduct.entity.dto.DeleteProductDto;
import com.rushb.rushbproduct.entity.dto.InsertProductDto;
import com.rushb.rushbproduct.entity.Product;
import com.rushb.rushbproduct.entity.dto.SelectProductDto;
import com.rushb.rushbproduct.entity.dto.UpdateProductDto;
import com.rushb.rushbproduct.mapper.ProductMapper;
import com.rushb.rushbproduct.result.Result;
import com.rushb.rushbproduct.service.ProductService;
import com.rushb.rushbproduct.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * rush-b</n>
 * ProductServiceimpl
 * </p>
 *
 * @author zhaolun.li
 * @since 2020/11/30 17:04
 */
@Slf4j
@Service
public class ProductServiceimpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public Result<List<Product>> selectAllProduct() {
        List<Product> list = productMapper.selectList(new QueryWrapper<Product>().eq("del_flag","0"));
        return new Result<>(list);
    }

    @Override
    public Result<List<Product>> selectProduct(SelectProductDto product) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        List<Product> list = new ArrayList<>();
        if(!BeanUtil.isEmpty(product)){
            if(product.getId() != null && !product.getId().equals("")){
                wrapper.eq("id",product.getId());
            }  if (product.getProdName() != null && !product.getProdName().equals("")){
                wrapper.eq("prod_name",product.getProdName());
            }  if (product.getProdLink() != null && !product.getProdLink().equals("")){
                wrapper.eq("prod_link",product.getProdLink());
            }  if (product.getProdSpec() != null && !product.getProdSpec().equals("")){
                wrapper.eq("prod_spec",product.getProdSpec());
            }  if (product.getProdPlat() != null && !product.getProdPlat().equals("")){
                wrapper.eq("prod_plat",product.getProdPlat());
            }  if (product.getProdPrice() != null && !product.getProdPrice().equals("")){
                wrapper.eq("prod_price",product.getProdPrice());
            }  if (product.getProdType() != null && !product.getProdType().equals("")){
                wrapper.eq("prod_type",product.getProdType());
            }  if (product.getDelFlag() != null && !product.getDelFlag().equals("")){
                wrapper.eq("del_flag",product.getDelFlag());
            }  if (product.getCreateUser() != null && !product.getCreateUser().equals("")){
                wrapper.eq("create_user",product.getCreateUser());
            }  if (product.getUpdateUser() != null && !product.getUpdateUser().equals("")){
                wrapper.eq("update_user",product.getUpdateUser());
            }  if(product.getCreateBeginTime() != null && !product.getCreateBeginTime().equals("")){
                if (product.getCreateEndTime() != null && !product.getCreateEndTime().equals("")){
                    wrapper.between("create_time",product.getCreateBeginTime(),product.getCreateEndTime());
                } else {
                    wrapper.ge("create_time",product.getCreateBeginTime());
                }
            }  if(product.getUpdateBeginTime() != null && !product.getUpdateBeginTime().equals("")){
                if (product.getUpdateEndTime() != null && !product.getUpdateEndTime().equals("")){
                    wrapper.between("update_time",product.getUpdateBeginTime(),product.getUpdateEndTime());
                } else {
                    wrapper.ge("update_time",product.getUpdateBeginTime());
                }
            }
            list = productMapper.selectList(wrapper);
        }
        return new Result<>(list);
    }

    @Override
    public Result<String> insertProduct(InsertProductDto dto) {
        Product product = new Product();
        if (dto.getId() != null && !dto.getId().equals("")){
            return new Result<>(ErrorCode.A0008);
        } else {
            BeanUtils.copyProperties(dto,product);
            return new Result<>(JSON.toJSONString(productMapper.insert(product)));
        }
    }

    @Override
    public Result<String> updateProduct(UpdateProductDto dto) {
        Product product = new Product();
        if (dto.getId() != null && !dto.getId().equals("")){
            BeanUtils.copyProperties(dto,product);
            return new Result<>(JSON.toJSONString(productMapper.updateById(product)));
        } else {
            return new Result<>(ErrorCode.A0007);
        }
    }

    @Override
    public Result<String> deleteProduct(DeleteProductDto dto) {
        Product product = new Product();
        if (dto.getId() != null && !dto.getId().equals("")){
            product.setId(dto.getId());
            product.setDelFlag("1");
            return new Result<>(JSON.toJSONString(productMapper.updateById(product)));
        } else {
            return new Result<>(ErrorCode.A0007);
        }
    }

}
